package com.jeesite.modules.ch.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.NumberUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ch.entity.ChChannel;
import com.jeesite.modules.ch.entity.ChJiesuan;
import com.jeesite.modules.ch.entity.param.JiesuanShuilv;
import com.jeesite.modules.ch.entity.param.JiesuanStatus;
import com.jeesite.modules.ch.entity.param.JiesuanType;
import com.jeesite.modules.ch.service.ChChannelService;
import com.jeesite.modules.ch.service.ChJiesuanService;
import com.jeesite.modules.ch.service.ChLiushuiService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 渠道结算Controller
 * @author 李寒冰
 * @version 2025-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/ch/chJiesuan")
public class ChJiesuanController extends BaseController {

	@Autowired
	private ChJiesuanService chJiesuanService;
    @Autowired
    private ChChannelService chChannelService;
    @Autowired
    private ChLiushuiService chLiushuiService;

	@Value("${pic.meitiH5AdminUrl}")
	private String meitiH5AdminUrl;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ChJiesuan get(String id, boolean isNewRecord) {
		return chJiesuanService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ch:chJiesuan:view")
	@RequestMapping(value = {"list", ""})
	public String list(ChJiesuan chJiesuan, Model model) {
		model.addAttribute("types", JSONObject.toJSONString(JiesuanType.getByMap()));
		model.addAttribute("shuilvs", JSONObject.toJSONString(JiesuanShuilv.getByMap()));
		model.addAttribute("statusArr", JSONObject.toJSONString(JiesuanStatus.getByMap()));
		model.addAttribute("chJiesuan", chJiesuan);
		return "modules/ch/chJiesuanList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ch:chJiesuan:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ChJiesuan> listData(ChJiesuan chJiesuan, HttpServletRequest request, HttpServletResponse response) {
		chJiesuan.setPage(new Page<>(request, response));
		Page<ChJiesuan> page = chJiesuanService.findPage(chJiesuan);
		for (ChJiesuan chJiesuan1 : page.getList()) {
			String fapiaoUrl = chJiesuan1.getFapiaoUrl();
			chJiesuan1.setFapiaoUrl(meitiH5AdminUrl + fapiaoUrl);
		}
		return page;
	}

	/**
	 * 修改状态
	 */
	@RequiresPermissions("ch:chJiesuan:view")
	@RequestMapping(value = "updateStatus")
	@ResponseBody
	public String updateStatus(ChJiesuan chJiesuan, HttpServletRequest request, HttpServletResponse response) {
		chJiesuanService.updateJiesuanStatus(chJiesuan);
		return renderResult(Global.TRUE, text("操作成功"));
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ch:chJiesuan:view")
	@RequestMapping(value = "form")
	public String form(ChJiesuan chJiesuan, Model model) {
		String fapiaoUrl = chJiesuan.getFapiaoUrl();
		chJiesuan.setFapiaoUrl(meitiH5AdminUrl + fapiaoUrl);
		model.addAttribute("chJiesuan", chJiesuan);
		model.addAttribute("types", JiesuanType.values());
		model.addAttribute("shuilvs", JiesuanShuilv.values());
		return "modules/ch/chJiesuanForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ch:chJiesuan:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ChJiesuan chJiesuan) {
		Double jiesuanJine = chJiesuan.getJiesuanJine();
		if (jiesuanJine==null ||  jiesuanJine==0 ){
			return renderResult(Global.FALSE, text("请填写结算金额！"));
		}
		if (jiesuanJine==null ||  jiesuanJine<100 ){
			return renderResult(Global.FALSE, text("结算金额必须不少于100！"));
		}
		User user = UserUtils.getUser();
		String userCode = user.getUserCode();
		ChChannel chChannel = chChannelService.getByUserCode(userCode);
		String id = chChannel.getId();
		double yue = chLiushuiService.yue(id);
		if(jiesuanJine>yue){
			return renderResult(Global.FALSE, text("可结算金额不足！"));
		}
		String jiesuanType = chJiesuan.getJiesuanType();
		if (jiesuanType.equals(JiesuanType.DUIGONG.getCode())){
			if(StringUtils.isEmpty(chJiesuan.getCompanyName()) || StringUtils.isEmpty(chJiesuan.getBankName()) || StringUtils.isEmpty(chJiesuan.getBankCode())
					|| StringUtils.isEmpty(chJiesuan.getFapiaoUrl())
			){
				return renderResult(Global.FALSE, text("请完善对公必要信息！"));
			}
			String shuilv = chJiesuan.getShuilv();
			chJiesuan.setActualJine(NumberUtils.div(NumberUtils.mul(jiesuanJine,100-(6-Integer.valueOf(shuilv))),100,2));
		}else{
			if(StringUtils.isEmpty(chJiesuan.getAlipayCode()) || StringUtils.isEmpty(chJiesuan.getAlipayName())){
				return renderResult(Global.FALSE, text("请完善对私必要收款信息！"));
			}
			chJiesuan.setActualJine(NumberUtils.div(NumberUtils.mul(jiesuanJine,94),100,2));
		}
		if(StringUtils.isEmpty(chJiesuan.getId())){
			chJiesuan.setJiesuanCode(chJiesuanService.getCode());
		}

		chJiesuanService.save(chJiesuan);
		return renderResult(Global.TRUE, text("提交渠道结算成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ch:chJiesuan:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ChJiesuan chJiesuan) {
		chJiesuanService.delete(chJiesuan);
		return renderResult(Global.TRUE, text("删除渠道结算成功！"));
	}
	
}